﻿# -*- coding: utf-8 -*-

import urllib2
import re
import codecs
from reader import *

# Import JPYPE
import jpype
import os
from jpype import java
from jpype import *
#Library for RSS 
import feedparser
import news

import rssFeedReader
#Resources's file.
import imgs.icons_rc 
#SQLite database
import db.db
from PyQt4 import QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtNetwork,Qt
from ui.main import Ui_MainWindow
from feedSubscribe import FeedSubscribeWidget

from customClassifier.classifier import CustomClassifier

import confOS

class MainWindow(QMainWindow):
	def setup(self):
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

	def __init__(self, parent = None):

		# self.categories = ["Ciencia y Tecnologia","Deportes","Economia","Espectaculos","Informacion General","Internacional","Politica"]
		self.categories = os.listdir(os.getcwd() + os.sep + "data")
		self.customClassifier = CustomClassifier(os.getcwd(),self.categories)
		self.table='<table border="1" summary="Clasificacion."><tr><th>Noticia</th><th>Custom partes</th><th>Custom</th><th>Weka partes</th><th>Weka</th></tr><tr>'
		self.cant = 0
		self.weigth = 0
		self.prom = 0
		self.STRING_SIZE = 500
		super(MainWindow, self).__init__(parent)
		self.setup()
		self.loadCategories()
		self.setIcons()
		#FeedSubscribeWidget object
		self.newFeedDialog = FeedSubscribeWidget()
		#Dictionary in which Feed is a key and QThread is a value
		self.feeds = {}
		# self.loadFeed()
		self.newsClassifier=None
		self.db = db.db.NewsDB(os.getcwd())
		#self.loadStoredNews()
		
		#-----QVBoxLayout----#
		self.vLayoutCienciaYTecnologia = QVBoxLayout()
		self.vLayoutDeportes = QVBoxLayout()
		self.vLayoutEconomia = QVBoxLayout()
		self.vLayoutEspectaculos = QVBoxLayout()		
		self.vLayoutInformacionGeneral = QVBoxLayout()
		self.vLayoutInternacional = QVBoxLayout()
		self.vLayoutPolitica = QVBoxLayout()
		
		self.loadStoredNews()
		#-----QVBoxLayout----#
		QObject.connect(self.ui.btnBack, SIGNAL("clicked()"), self.backButton)
		QObject.connect(self.ui.menuSubscribe, SIGNAL("triggered()"), self.openFeedSubscribeWidget)
		QObject.connect(self.newFeedDialog,SIGNAL("newSubscribe(PyQt_PyObject)"),self.loadFeed)
		QObject.connect(self.ui.treeWidgetFeed,SIGNAL('itemDoubleClicked(QTreeWidgetItem*, int)'),self.openLinkFromItem)		

	def openLinkFromItem(self,item,int):
		'''print item.text(1)'''
		if item.text(1) != "":
			self.openLink(item.text(1))
		else:
			category = item.text(0)
			if  category == u"Politica":				
				self.ui.scrollArea.setLayout(self.vLayoutPolitica)
			elif category == u"Economía":				
				self.ui.scrollArea.setLayout(self.vLayoutEconomia)
			elif category == u"Ciencia y Tecnologia":				
				self.ui.scrollArea.setLayout(self.vLayoutCienciaYTecnologia)
			elif category == u"Espectaculos":			
				self.ui.scrollArea.setLayout(self.vLayoutEspectaculos)
			elif category == u"Deportes":				
				self.ui.scrollArea.setLayout(self.vLayoutDeportes)
			elif category == u"Internacional":				
				self.ui.scrollArea.setLayout(self.vLayoutInternacional)
			elif category == u"Informacion General":				
				self.ui.scrollArea.setLayout(self.vLayoutInformacionGeneral)

	#Metodo que carga las noticias almacenadas en la base de datos. 
	#Este metodo se ejecuta al iniciarse la aplicación
	def loadStoredNews(self):
		storedNews = self.db.loadNews()
		for sn in storedNews:
			self.insertNewsInCategory(sn[4],sn[0],sn[2])
			#acá se debería almacenar en un verticalLayout para cada categoria.
			self.addNewsInVBoxLayout(sn[0],sn[1],sn[2],sn[3],sn[4])

	def remove_html_tags(self,data):
		#p = re.compile(r'<.*?>')
		p = re.compile(r'<[^<]*?/?>')
		return p.sub('', data)

	def remove_extra_spaces(self,data):
		p = re.compile(r'\s+')
		return p.sub(' ', data)
		
	def closeEvent (self, QCloseEvent):
		self.table=self.table+'</table>'
		f = open('Clasificacion.html', 'a')
		f.write(unicode(self.table).encode("utf-8"))
		f.close()

	def classifyText(self,data,cl,parts):
	
		if(not parts):
			if(cl=="weka"):
				category = self.newsClassifier.classify(self.String(data))
			else:
				category = self.customClassifier.classify(data)
			return category
	
	
		categoryAnt=''
		start=0
		cant = self.STRING_SIZE
		sup=start+cant
		start=sup
		sup=start+cant
		list=[]
		count=0
		while(len(data)>sup+cant):
			input = data[start:sup]
			sup=sup+cant
			if(cl=="weka"):
				category = self.newsClassifier.classify(self.String(input))
			else:
				category = self.customClassifier.classify(input)
			list.append(category)
			if(category!=categoryAnt):
				count=1
				categoryAnt=category
			else:
				count=count+1
			if(count==3):
				break;
		category=categoryAnt
		if (category=="" or count<3):
			if(cl=="weka"):
				category = self.newsClassifier.classify(self.String(input))
			else:
				category = self.customClassifier.classify(input)
		return category
		
	def loadNews(self,link):
		reader = Reader(link)
		return reader.getText()
	
	
	def classifyWeka(self,title,summary,data,parts):
		
		if(self.newsClassifier==None):
			self.NewsClassifier=jpype.JClass('classifier.NewsClassifier')
			self.String = jpype.JClass('java.lang.String')
			self.newsClassifier = self.NewsClassifier.createFromModel(self.String("modelo.model"))

		category=''
		category=self.classifyText(data,"weka",parts)
		
		'''if (category=="" or count<3):
			category = self.newsClassifier.classifyMessage(self.String(data))
		'''
		
		return unicode(category).encode("utf-8")
	
	def classifyCustom(self,title,sumary,data,parts):
		print "en custom"
		category = self.classifyText(data,"custom",parts)
		return unicode(category).encode("utf-8")

	#Metodo que inicializa el arbol de categorias.
	def loadCategories(self):
		self.ui.treeWidgetFeed.clear()
		self.ui.treeWidgetFeed.setHeaderLabels(["Categoria"])

		for c in self.categories:
			item = QTreeWidgetItem(self.ui.treeWidgetFeed)
			item.setText(0,c)
			self.ui.treeWidgetFeed.addTopLevelItem(item);

	#Metodo que se ejecuta al evento producido al agregar un nuevo feed.
	def loadFeed(self,feed):
		feed = str(feed)
		if not(feed in self.feeds):
			self.feeds[feed] = rssFeedReader.UpdateFeedThread(self.ui,feed)
			(self.feeds[feed]).start()
			QObject.connect(self.feeds[feed],SIGNAL("asignal(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"),self.addNews)

	#Method that set the icons in the Form
	def setIcons(self):
		self.ui.btnBack.setIcon(QIcon(":/backButton"))
		self.ui.menuSubscribe.setIcon(QIcon(":/newFeed"))

	def makeHtmlTable(self, results):
		
		rows='<tr>'
		rows = rows + '<td>'+results[0]+'</td>' 
		rows = rows + '<td>'+results[1]+'</td>'
		rows = rows + '<td>'+results[2]+'</td>' 
		rows = rows + '<td>'+results[3]+'</td>' 
		rows = rows + '<td>'+results[3]+'</td>'
		row='</tr>'
		self.table=self.table+rows

		




	#Método encargado de crear una instancia de la clase News y de insertarla en el verticalLayoutNews
	def addNews(self,title,update,summary,link):
		print "aca"
		if(link==''): 
			return
		data = self.loadNews(link)
		results=['','','','','']
		
		results[0]=  title
		
		results[1] = self.classifyCustom(title,summary,data,True)
		results[2] = self.classifyCustom(title,summary,data,False)
		results[3] = self.classifyWeka(title,summary,data,True)
		results[4] = self.classifyWeka(title,summary,data,False)
		category=results[1]
		self.makeHtmlTable(results)
		'''out= title+'=> Custom partes: '+category+', Custom: '+category1+', Weka partes: '+category2+', Weka: '+category3+'\n\n';'''
		
		if(category==''): return
		# print "Texto a clasificar: " + title + " " + summary
		
		# reader = Reader(link)
		# data = reader.getText()
		
		# print "category: " + category + " - title: " + title #+ " - Segundo metodo: " + unicode((self.customClassifier.classify(data)).encode("utf-8"))
		# category = unicode(self.customClassifier.classify(data)).encode("utf-8")
		
		self.insertNewsInCategory(category,title,link)

		self.db.addNews(title,summary,link,update,category)
	
		
		
		
		
		
		# Create an instance of News Widget
		# newss = news.NewsWidget(title, update, summary, link)
		# self.ui.verticalLayoutNews.addWidget(newss)
		# QObject.connect(newss,SIGNAL("doubleClickNews(PyQt_PyObject)"),self.openLink)
		self.addNewsInVBoxLayout(title,summary,link,update,category)

	def addNewsInVBoxLayout(self,title,summary,link,update,category):
		# Create an instance of News Widget
		newss = news.NewsWidget(title, update, summary, link)
		QObject.connect(newss,SIGNAL("doubleClickNews(PyQt_PyObject)"),self.openLink)

		if category == u"Politica":
			self.vLayoutPolitica.addWidget(newss)
		elif category == u"Economia":
			self.vLayoutEconomia.addWidget(newss)
		elif category == u"Ciencia y Tecnologia":
			self.vLayoutCienciaYTecnologia.addWidget(newss)
		elif category == u"Espectaculos":
			self.vLayoutEspectaculos.addWidget(newss)
		elif category == u"Deportes":
			self.vLayoutDeportes.addWidget(newss)
		elif category == u"Internacional":
			self.vLayoutInternacional.addWidget(newss)
		elif category == u"Informacion General":
			self.vLayoutInformacionGeneral.addWidget(newss)

	#Metodo que agrega una noticia en el arbol de categorias.
	def insertNewsInCategory(self,category,title,link):
		item = QTreeWidgetItem(self.ui.treeWidgetFeed.findItems(category,QtCore.Qt.MatchExactly)[0])
		item.setText(0,title)
		item.setText(1,link)
		self.ui.treeWidgetFeed.addTopLevelItem(item)

	#Metodo que abre un link en un QWebView
	def openLink(self, link):
		self.ui.webView.load(QUrl(link))
		self.ui.stackedWidget.setCurrentIndex(0)

	#Metodo que regresa a la vista del listado de noticias.
	#Este metodo es ejecutado al apretar el boton Back.
	def backButton(self):
		self.ui.webView.back()
		self.ui.stackedWidget.setCurrentIndex(1)

	#Metodo que abre la ventana para agregar un nuevo feed.
	#Este metodo se ejecuta al hacer click en File->Subscribe.
	def openFeedSubscribeWidget(self):
		self.newFeedDialog.exec_()


import sys

app = QApplication(sys.argv)
_jvmArgs = ["-ea"] # enable assertions
_jvmArgs.append("-Dfile.encoding=utf-8")
_jvmArgs.append("-Djava.class.path="+os.environ["CLASSPATH"])
jpype.startJVM(confOS.javaPath, *_jvmArgs)
main = MainWindow()
main.show()

sys.exit(app.exec_())